package com.sander.guardmanagement.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.UserManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sander.guardmanagement.R;
import com.sander.guardmanagement.http.BaseCallback;
import com.sander.guardmanagement.http.HttpApi;
import com.sander.guardmanagement.http.OkHttpHelper;
import com.sander.guardmanagement.http.UserEditPassword;
import com.sander.guardmanagement.http.UserReg;
import com.sander.guardmanagement.http.UserSendsms;
import com.sander.guardmanagement.response.ResponseModel;
import com.sander.guardmanagement.response.ResponseUserReg;
import com.sander.guardmanagement.response.ResponseUserSendsms;
import com.sander.guardmanagement.until.AppUnit;
import com.sander.guardmanagement.until.UserInfoManager;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.Timer;
import java.util.TimerTask;

import bolts.Task;

public class RegisteredActivity extends AppCompatActivity {

    private String sms_id;

    private int timeCount = 300;


    boolean isChangePassworld;

    private Timer mtimer;
    LinearLayout regiestView;

    //注册模块的
    EditText input_user_name_regiest_editview;
    TextView get_smsCode_button;
    EditText input_user_sms_code_editview;
    EditText input_user_password_regiest_editeView;
    Button regiest_action_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        Intent intent = getIntent();
        isChangePassworld = intent.getBooleanExtra("ischange",true);
        mtimer = new Timer();
        initCallBack();
        initData();
        initView();
    }

    private void initCallBack(){
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.callbackLayout);
        frameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void initData(){

    }
    public void initView(){

        input_user_name_regiest_editview = (EditText)findViewById(R.id.inputusernameedittext) ;
        get_smsCode_button = (TextView) findViewById(R.id.tv_get_smscode) ;
        input_user_sms_code_editview = (EditText) findViewById(R.id.et_reg_smscode) ;
        input_user_password_regiest_editeView = (EditText)findViewById(R.id.et_reg_password);

        regiest_action_button = (Button) findViewById(R.id.reg_confirm) ;
        regiest_action_button.setText("修改密码");

        regiestView = (LinearLayout)findViewById(R.id.registerView);

        get_smsCode_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onGetSMSCode();
            }
        });

        regiest_action_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeUserInfo();
            }
        });

        regiestView.setVisibility(View.VISIBLE);
    }

    public void setTimerTask(){
        mtimer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                timerHandler.sendMessage(message);
            }
        },1000,1000);
    }


    public void onGetSMSCode(){
        String phone_string = input_user_name_regiest_editview.getText().toString();
        if (TextUtils.isEmpty(phone_string)){
            showToastMessage("用户名不能为空");
            return;
        }

        UserSendsms userSendsms = new UserSendsms(phone_string);

        OkHttpHelper.getinstance().post(HttpApi.getAPiUrl(HttpApi.SMS_verification_code), userSendsms.getValueOfClas(), new BaseCallback<ResponseUserSendsms>() {
            @Override
            public void onRequestBefore() {
                Log.d("sander","on request before");
            }

            @Override
            public void onFailure(Request request, Exception e) {
                Log.d("sander","on failure");
                regiest_action_button.setEnabled(false);
            }

            @Override
            public void onSuccess(Response response, ResponseUserSendsms responseUserSendsms) {
                if (responseUserSendsms.getCode() == 0){
                    Log.d("sander",responseUserSendsms.msg);
                    Log.d("sander",responseUserSendsms.getData().getPhone());
                    sms_id = responseUserSendsms.getData().getSms_id();
                    startCountdown();
                }else{
                    showToastMessage(responseUserSendsms.getMsg());

                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    public void changeUserInfo(){
        String phone_string = input_user_name_regiest_editview.getText().toString();
        if (TextUtils.isEmpty(phone_string)){
            showToastMessage("用户名不能为空");
        }

        String password_string = input_user_password_regiest_editeView.getText().toString();
        if (TextUtils.isEmpty(password_string)){
            showToastMessage("验证码不能为空");
        }

        String sms_code_string = input_user_sms_code_editview.getText().toString();
        if (TextUtils.isEmpty(sms_code_string)){
            showToastMessage("密码不能为空");
        }

        UserEditPassword userEditPassword = new UserEditPassword();
        userEditPassword.phone = phone_string;
        userEditPassword.password = AppUnit.getSha1(password_string);
        userEditPassword.sms_id = sms_id;
        userEditPassword.sms_code = sms_code_string;
        OkHttpHelper.getinstance().post(userEditPassword.requestUrl(), userEditPassword.getValueOfClas(), new BaseCallback<ResponseModel>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, ResponseModel o) {
                if (o.getCode() == 0){
                    finish();
                }else{
                    showToastMessage(o.getMsg());
                }

            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }
    public void startCountdown(){
        regiest_action_button.setEnabled(true);
        changeSmsButton();
        get_smsCode_button.setEnabled(false);
        setTimerTask();

    }

    public void changeSmsButton(){
        get_smsCode_button.setText(timeCount + "秒重发");

    }

    private Handler timerHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1){
                timeCount --;
                if (timeCount >= 0){
                    changeSmsButton();
                }else{
                    mtimer.cancel();
                    get_smsCode_button.setEnabled(true);
                    get_smsCode_button.setText(R.string.getsmsversion);
                }
            }
        }
    };
    public void showToastMessage(String message){
        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_SHORT).show();

    }

}
